/** @format */
import Vue from 'vue'
import Router from 'vue-router'
import NavBar from '../components/NavBar'
import { getAccessToken } from '@/utils/userAuth'

const scrollBehavior = (to, from, savedPosition) => {
  // console.log(to, from, savedPosition);
  if (savedPosition) {
    return savedPosition
  } else {
    const position = { x: 0, y: 0 }
    if (to.hash) {
      position.selector = to.hash
    }
    return position
  }
}

Vue.use(Router)
const router = new Router({
  // mode: 'history',
  base: '/zhongbao-static/', //如果项目根目录不为域名，则添加该行
  routes: [
    {
      path: '/',
      component: NavBar,
      redirect: '/home',
      children: [
        {
          path: 'home',
          name: '首页',
          component: () => import(/* webpackChunkName: "home" */ '../views/home'),
          meta: { title: '首页' }
        },
        {
          path: '/my',
          name: '我的',
          component: () => import(/* webpackChunkName: "my" */ '../views/my'),
          meta: { title: '我的' }
        }
      ]
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import(/* webpackChunkName: "login" */ '../views/login'),
      meta: { title: '登录/注册' }
    },
    {
      path: '/logout',
      name: '退出登录',
      component: () => import(/* webpackChunkName: "login" */ '../views/logout'),
      meta: { title: '退出登录' }
    },
    {
      path: '/releaseTask',
      name: '发布任务',
      component: () => import(/* webpackChunkName: "releaseTask" */ '../views/releaseTask'),
      meta: { title: '发布任务', requireAuth: true, requireIdentity: true }
    },
    {
      path: '/improvementPersonal',
      name: '完善资料',
      component: () =>
        import(
          /* webpackChunkName: "improvementPersonal" */
          '../views/my/user/improvementPersonal'
        ),
      meta: { title: '完善资料', requireAuth: true }
    },
    {
      path: '/realName',
      name: '实名认证',
      component: () =>
        import(
          /* webpackChunkName: "realName" */
          '../views/my/user/realName'
        ),
      meta: { title: '实名认证', requireAuth: true }
    },
    {
      path: '/improvementCompany',
      name: '完善资料',
      component: () =>
        import(
          /* webpackChunkName: "improvementCompany" */
          '../views/my/company/improvementCompany'
        ),
      meta: { title: '完善资料', requireAuth: true }
    },
    {
      path: '/haveTask',
      name: '已接任务',
      component: () =>
        import(
          /* webpackChunkName: "haveTask" */
          '../views/my/user/haveTask'
        ),
      meta: { title: '已接任务', requireAuth: true }
    },
    {
      path: '/jobDetail/:id?', // flag：（默认不传）从用户已接任务进入传true
      name: '任务详情',
      component: () =>
        import(
          /* webpackChunkName: "haveTaskDetail" */
          '../views/my/common/jobDetails'
        ),
      meta: { title: '任务详情' }
    },
    {
      path: '/haveTaskDetail/:id?/:flag?', // flag：（默认不传）从用户已接任务进入传true
      name: '已接任务详情',
      component: () =>
        import(
          /* webpackChunkName: "haveTaskDetail" */
          '../views/my/user/haveTaskDetail'
        ),
      meta: { title: '已接任务详情' }
    },
    {
      path: '/haveTaskReportDetails/:userJobReportId',
      name: '汇报详情',
      component: () =>
        import(
          /* webpackChunkName: "haveTaskReportDetails" */
          '../views/my/user/haveTaskReportDetails'
        ),
      meta: { title: '汇报详情', requireAuth: true }
    },
    {
      path: '/issuedTask',
      name: '已发布任务',
      component: () =>
        import(
          /* webpackChunkName: "issuedTask" */
          '../views/my/company/issuedTask'
        ),
      meta: { title: '已发布任务', requireAuth: true }
    },
    {
      path: '/issuedTaskDetail/:jobId',
      name: '已发布详情',
      component: () =>
        import(
          /* webpackChunkName: "issuedTaskDetail" */
          '../views/my/company/issuedTaskDetail'
        ),
      meta: { title: '已发布详情', requireAuth: true }
    },
    {
      path: '/costDetail/:jobId',
      name: '费用列表',
      component: () =>
        import(
          /* webpackChunkName: "issuedTaskDetail" */
          '../views/my/company/costDetail'
        ),
      meta: { title: '费用列表', requireAuth: true }
    },
    {
      path: '/checkPersonal',
      name: '接单人资料',
      component: () =>
        import(
          /* webpackChunkName: "issuedTaskDetail" */
          '../views/my/company/checkPersonal'
        ),
      meta: { title: '接单人资料', requireAuth: true }
    },
    {
      path: '/progressReport',
      name: '进度汇报',
      component: () =>
        import(
          /* webpackChunkName: "progressReport" */
          '../views/my/company/progressReport'
        ),
      meta: { title: '进度汇报', requireAuth: true }
    },
    {
      path: '/reportList/:userJobId',
      name: '汇报列表',
      component: () =>
        import(
          /* webpackChunkName: "reportList" */
          '../views/my/company/reportList'
        ),
      meta: { title: '汇报列表', requireAuth: true }
    },
    {
      path: '/reportDetails/:userJobReportId',
      name: '汇报详情',
      component: () =>
        import(
          /* webpackChunkName: "reportDetails" */
          '../views/my/company/reportDetails'
        ),
      meta: { title: '汇报详情', requireAuth: true }
    },
    {
      path: '/uploadReport/:userJobId/:userJobStatus',
      name: '上传汇报',
      component: () =>
        import(
          /* webpackChunkName: "uploadReport" */
          '../views/my/user/uploadReport'
        ),
      meta: { title: '上传汇报', requireAuth: true }
    },
    {
      path: '/userprogressReport/:userJobId?/:userJobStatus?',
      name: '进度汇报',
      component: () =>
        import(
          /* webpackChunkName: "progressReport" */
          '../views/my/user/userProgressReport'
        ),
      meta: { title: '进度汇报', requireAuth: true }
    },
    {
      path: '/recommendCompany',
      name: '合作企业',
      component: () =>
        import(
          /* webpackChunkName: "progressReport" */
          '../views/my/recommend'
        ),
      meta: { title: '合作企业', requireAuth: true }
    },
    {
      path: '/recommendCompany/:id',
      name: '合作企业详情',
      component: () =>
        import(
          /* webpackChunkName: "progressReport" */
          '../views/my/recommend/detail'
        ),
      meta: { title: '合作企业详情', requireAuth: true }
    }
  ],
  scrollBehavior
})

router.beforeEach((to, from, next) => {
  // console.log(to, 'to')
  // console.log(from, 'from')
  const { companyUserDetail } = router.app.$options.store.state.myData
  // console.log(companyUserDetail, '11111111')
  /* 页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  /* 判断该路由是否需要登录权限 */
  if (to.matched.some(record => record.meta.requireAuth)) {
    //根据是否有token判断是否已登录
    if (getAccessToken()) {
      next()
    } else {
      next({
        path: `/login`
      })
    }
  }

  /* 判断用户是否有完善资料判断 */
  if (to.matched.some(record => record.meta.requireIdentity)) {
    const { auditStatus } = companyUserDetail.zbCompanyResponse
    // console.log(auditStatus, 'auditStatus')
    //如果完善资料状态为未提交，跳转到完善资料页面
    if (auditStatus == 'EDITING') {
      next({
        path: '/improvementCompany'
      })
    } else {
      next()
    }
  }

  if (to.path == '/login') {
    //已登录状态下请求登录页，自动跳转到首页
    if (getAccessToken()) {
      next({
        path: '/'
      })
    } else {
      next()
    }
  }

  next()
})

console.log(router)

export default router
